/*****************************************************************************************
 **  Author:jxx 2022
 **  QQ:283591387
 **完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
 **常用示例见：http://v2.volcore.xyz/document/vueDev
 **后台操作见：http://v2.volcore.xyz/document/netCoreDev
 *****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import modelHeader from "../produce_use_tj/produceUseTj.vue"
import gridHeader from "@/components/print/selectTemplet.vue"
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader,
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: modelHeader,
    modelBody: '',
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: {
    view: [],
    box: [],
    detail: []
  }, //扩展的按钮
  methods: {
    //下面这些方法可以保留也可以删除
    async onInit() {
      this.queryFields = ["Proposer", "CreateDate"]
      let startTime = await this.base.getOneWeekAgoDate()
      let endTime = await this.base.getDate(false) + " 23:59:59"
      this.searchFormFields.CreateDate = [startTime, endTime]
      this.detailOptions.textInline = false
      this.textInline = false
      // this.editFormOptions.forEach(x => {
      //   x.forEach(item => {
      //     if (item.field == 'AccessoryOne'||item.field == 'AccessoryTwo') {
      //       item.multiple = true;
      //       item.maxFile = 10;
      //     }
      //   })
      // })
      if (this.$route.path == "/q_produce_notarize" && JSON.stringify(this.$route.query) != '{}') {
        console.log("触发查询")
        await this.base.setSearchParamFn(this)
        // this.searchFormFields.Voucher_sub = null
        // this.searchFormFields.Proposer = null
        // this.searchFormFields.AuditStatus = null
        // this.searchFormFields.CreateDate = [null, null]
        // const selectList = ['AuditStatus', 'CreateDate',]
        // this.searchFormOptions.forEach(item => {
        //   item.forEach(x => {
        //     if (selectList.includes(x.field)) {
        //       x.onChange = (v) => {
        //         this.$route.query = {}
        //       }
        //     }
        //   })
        // })
        setTimeout(() => {
          this.refresh()
        })
      }
    },
    onInited() {
      // this.setFiexdSearchForm(true);
      // this.buttons.forEach(item => {
      //   if (item.name == "高级查询") {
      //     item.hidden = true
      //   }
      // })
      let indexNum = this.detailOptions.buttons.findIndex(item => item.name == "添加行")
      if (indexNum != "-1") {
        this.detailOptions.buttons.splice(indexNum, 1)
      }
      this.detailOptions.buttons.unshift({
        name: "领用单统计",
        icon: 'el-icon-plus',
        type: 'primary',
        plain: true,
        onClick: function () {
          if (this.rowschild && this.$refs.detail.rowData.length) {
            this.rowschild.click()
          }
          if (!this.editFormFields.WHSE_no) return this.base.ElMessageFn('请选择生产机构');
          this.$store.commit("setTabelText", true)
          this.$refs.modelHeader.open(this.editFormFields.Proposer, this.editFormFields.WHSE_no)
        }
      })

      //获取图片位置，在图片后面加一个上传按钮,这里只是演示随便找的一个字段存图片
      let column = this.detailOptions.columns.find(x => {
        return x.field == 'Attachment'
      });
      let column2 = this.detailOptions.columns.find(x => {
        return x.field == 'Image'
      });
      column.edit.autoUpload = true; //是否自动上传
      column.edit.multiple = true; //多文件上传
      column.edit.maxFile = 10; //最多上传10个文件
      column2.edit.autoUpload = true;
      column2.edit.multiple = true;
      column2.edit.maxFile = 10;


      this.detailOptions.beginEdit = (row, column, index) => {

        if (this.$route.path != "/q_produce_notarize" || this.rowAuditStatus != 0) { //|| !this.CreateUserBoole

          return false
        }
        let rows = document.querySelectorAll(".grid-detail .el-table__body .el-table__row")
        this.rowschild = rows[0].childNodes[2]
        this.detailOptions.columns.forEach(item => {
          if (item.field == "WHSE_name") {
            this.base.getWHSEName(item, row.Goods_sub, false, this.editFormFields.WHSE_no)
            // let selectUrl = `api/q_warehouse_locationList/getgoodslocation?bm=${row.Goods_sub}`
            // item.bind.data.forEach(d => {
            //   d.hidden = true
            // })
            // this.http.post(selectUrl, {}, "").then(reslut => {
            //   item.bind.data.forEach(d => {
            //     d.hidden = true
            //   })
            //   item.bind.data.forEach(d => {
            //     // reslut.rows.forEach(j => {
            //     //   if (d.key == j.WHSE_no) {
            //     //     d.hidden = false
            //     //   }
            //     // })
            //     reslut.forEach(j => {
            //       if (d.key == j.WHSE_no) {
            //         d.hidden = false
            //       }
            //     })
            //   })
            // })
          }
        })
        return true
      }
    },
    WHSE_IdDisableFn() {
      this.editFormOptions.forEach(item => {
        item.forEach(x => {
          if (x.field == 'WHSE_no') {
            x.disabled = true
          }
        })
      })
    },
    async searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      if (this.$route.path == "/q_produce_notarize" && JSON.stringify(this.$route.query) != '{}') {
        await this.base.setSearchBefore(this, "Produce_notarize", param)
        // const sectionState = ["[null,null]", JSON.stringify(["", null]), JSON.stringify([null, ""]), JSON.stringify(["", ""])]
        // const dateState = sectionState.includes(JSON.stringify(this.searchFormFields.CreateDate))
        // if (this.searchFormFields.Voucher_sub || this.searchFormFields.Goods_num || !dateState) {
        //   param.wheres = param.wheres.filter(item => item.name != "Produce_notarize")
        //   this.$route.query = {}
        // }
      }
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true;
    },
    delBefore(ids, rows) { //查询界面的表删除前 ids为删除的id数组,rows删除的行
      let auditStatus = rows.some(x => {
        return x.AuditStatus == 1
      });
      if (auditStatus) {
        this.base.ElMessageFn('不支持删除审核通过数据')
        return false;
      }
      return true;
    },
    addBeforeAsync(formData) {
      return this.testAdd(formData)
    },
    async updateBeforeAsync(formData) {
      return this.testAdd(formData)

    },
    delDetailRow(rows) { //弹出框删除明细表的行数据(只是对table操作，并没有操作后台)
      try {
        if (rows.length == this.$refs.detail.rowData.length) {
          this.$store.commit("setWHSE_time", true)
          this.$store.commit("setWHSE_disabled", true)
          this.WHSE_Interval = setInterval(() => {
            console.log("WHSE_Interval", this.WHSE_Interval)
            this.editFormOptions.forEach(item => {
              item.forEach(x => {
                if (x.field == 'WHSE_no') {
                  if (this.$store.state.WHSE_disabled) {
                    x.disabled = true
                  } else {
                    x.disabled = false
                  }
                }
              })
            })
            if (!this.$store.state.WHSE_time) {
              clearInterval(this.WHSE_Interval)
              console.log("WHSE_Interval清除", this.WHSE_Interval)
            }
          }, 500)

        }
      } catch {
        console.log("删除catch", )
      }
      return true;
    },
    rowClick({
      row,
      column,
      event
    }) {
      //查询界面点击行事件
      this.$refs.table.$refs.table.toggleRowSelection(row);
    },
    detailRowClick({
      row,
      column,
      event
    }) {
      this.$refs.detail.$refs.table.toggleRowSelection(row);
      if (this.$route.path != "/q_produce_notarize" || this.rowAuditStatus != 0) {
        return
      }; //|| !this.CreateUserBoole
      if (column.property == "Remark" || column.property == "SN") {
        let dom = event.target
        if (dom.nodeName != "SPAN") {
          setTimeout(() => {
            let inpDom = dom.querySelector("input")
            inpDom.focus()
          }, 0)
        }

      }
    },
    onModelClose(iconClick) {
      //iconClick=true为点击左中上角X触发的关闭事件
      //如果返回 false不会关闭弹出框 
      //return false;
      this.$store.commit("setTabelText", true)
      this.boxModel = false;
    },
    async modelOpenBefore(row) {
      this.$store.commit("setTabelText", false)
      this.CreateUserBoole = row.CreateID == this.$store.state.userID
      this.rowAuditStatus = await row.AuditStatus
      if (JSON.stringify(row) == "{}") {
        this.rowAuditStatus = 0
        this.CreateUserBoole = true
      }
      if (this.$route.path != "/q_produce_notarize" || this.rowAuditStatus != 0) { //|| !this.CreateUserBoole
        this.detailOptions.columns.forEach(item => {
          if (item.field == 'WHSE_name') {
            item.filter = true
          }
          if (item.field == "Attachment" || item.field == "Image") {
            item.readonly = true
          }
        })
        this.boxButtons[0].hidden = true
        this.detailOptions.buttons.forEach(item => {
          if (item.name != "导出" && item.name != "刷新") {
            item.hidden = true
          }
        })
      } else {
        this.detailOptions.columns.forEach(item => {
          if (item.field == 'WHSE_name') {
            item.filter = false
          }
          if (item.field == "Attachment" || item.field == "Image") {
            item.readonly = false
          }
        })
        this.boxButtons[0].hidden = false
        this.detailOptions.buttons.forEach(item => item.hidden = false)
      }
    },
    modelOpenAfter(row) {
      this.rowschild = null
      // console.log(this.detailOptions);

      this.editFormOptions.forEach(item => {
        item.forEach(x => {
          if (x.field == "Voucher_sub" || x.field == "Goods_num") {
            x.disabled = true
          } else {
            x.disabled = false
          }

          if (x.field == 'WHSE_no') {
            const bool = !(this.currentAction == 'Add')
            x.disabled = bool
          }
          if (this.$route.path != "/q_produce_notarize" || this.rowAuditStatus != 0) { //|| !this.CreateUserBoole
            x.disabled = true
            //控制图片上传
            // this.detailOptions.columns.forEach(item => {
            //   if (item.field == "AccessoryOne" || item.field == "AccessoryTwo") {
            //     item.readonly = true
            //     item.type = "img"
            //   }
            // })
          }
          // else {
          //   //控制图片上传
          //   this.detailOptions.columns.forEach(item => {
          //     if (item.field == 'WHSE_name') {
          //       item.filter = false
          //     }
          //     if (item.field == "AccessoryOne" || item.field == "AccessoryTwo") {
          //       item.readonly = false
          //       item.type = "img"
          //     }
          //   })
          // }
        })
      })
    },
    testAdd(formData) {
      if (this.currentAction == "Add") {
        formData.mainData.Goods_num = 0
      }
      if (!this.$refs.detail.rowData.length) {
        this.base.ElMessageFn('明细表不可为空', "error")
        return false
      }
      if (!this.editFormFields.WHSE_no) {
        this.base.ElMessageFn("请选择生产机构")
        return false;
      }
      // let boole2 = formData.detailData.every(item => item.SN)
      // if (!boole2) {
      //   this.base.ElMessageFn("明细表中SN码不可为空")
      //   return false;
      // }
      return true
    },
  }
};
export default extension;